package com.eggy648.moreeggs.common;

import net.minecraft.command.*;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;

import javax.annotation.Nullable;
import java.util.List;

/**
 * Created by Cheese on 2017/2/26.
 * boom someone
 */
public class CommandBoom extends CommandBase{
    /**
     * Gets the name of the command
     */
    @Override
    public String getCommandName() {
        return "boom";
    }

    /**
     * Gets the usage string for the command.
     *
     * @param sender
     */
    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "commands.boom.usage";
    }

    /**
     * Return the required permission level for this command.
     */
    @Override
    public int getRequiredPermissionLevel() {
        return 2;
    }

    @Override
    public List<String> getTabCompletionOptions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos pos) {
        if(args.length==1)
        {
            return CommandBase.getListOfStringsMatchingLastWord(args,server.getAllUsernames());
        }
        return null;
    }

    /**
     * Callback for when the command is executed
     *
     * @param server
     * @param sender
     * @param args
     */

    @Override
    public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
        if(args.length>1)
        {
            throw new WrongUsageException("commands.boom.usage");
        }
        else
        {
            EntityPlayerMP player;
            try
            {
                player  = args.length>0 ? CommandBase.getPlayer(server,sender,args[0]) :
                        CommandBoom.getCommandSenderAsPlayer(sender);
            }catch (PlayerNotFoundException e)
            {
                sender.addChatMessage(new TextComponentString("execute this command " +
                        "as a player or enter player name!"));
                return;
            }
            BlockPos pos=player.getPosition();
            player.worldObj.createExplosion(player,pos.getX(),pos.getY(),pos.getZ(),4.0F,false);
        }
    }
}
